﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Bosses.PB
{
    public class PumpkinBomb : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.width = 20;
            Projectile.height = 20;
            Projectile.aiStyle = 16;
            Projectile.friendly = false;
            Projectile.hostile = true;
            Projectile.penetrate = -1;
            Projectile.timeLeft = 300;
            Projectile.light = 0.5f;
        }

        public override void AI()
        {
            if (Main.rand.NextBool(2))
            {
                Dust.NewDust(Projectile.position, Projectile.width, Projectile.height,
                    DustID.Pumpkin, 0f, 0f, 100, default, 0.8f);
            }
        }


        public override void OnKill(int timeLeft)
        {
            SoundEngine.PlaySound(SoundID.Item14, Projectile.Center);
            for (int i = 0; i < 20; i++)
            {
                Dust.NewDust(Projectile.position, Projectile.width, Projectile.height,
                    DustID.Torch, 0f, 0f, 100, Color.Orange, 1.2f);
            }
            GeneratePumpkinGores(Projectile.Center, Projectile.width);
        }
        private void GeneratePumpkinGores(Vector2 position, int size)
        {
            for (int i = 0; i < 3; i++)
            {
                Vector2 gorePos = position + new Vector2(Main.rand.Next(-size / 2, size / 2), Main.rand.Next(-size / 2, size / 2));
                Vector2 goreVel = new(Main.rand.NextFloat(-3f, 3f), Main.rand.NextFloat(-3f, 3f));
                if (Main.netMode != NetmodeID.Server)
                {
                    Gore.NewGore(Projectile.GetSource_Death(), gorePos, goreVel, Mod.Find<ModGore>("Gore_1").Type, 1f);
                }
            }
        }
    }
}